package com.example.whm;


import org.holoeverywhere.app.Activity;
import org.holoeverywhere.app.Application;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.widget.LinearLayout;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.PopupMenu;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.example.whm.lib.box_Selection;

public class MainActivity extends Activity implements OnClickListener,box_Selection{
  
	AQuery aq;
	private TextView one;
	private TextView four;
	private TextView three;
	private TextView two;
	private TextView five;
	
	private OneFragment onef;
	private TwoFragment twof;
	private ThreeFragment threef;
	private FourFragment fourf;
	private FiveFragment fivef;
	private FourFragmentOrder onebox;
	private FourFragmentPreorder twobox;
	private FourFragmentSubscribe threebox;
	
	private FragmentManager fm;
	TextView title;
	View title_r;
	LinearLayout body;
	SharedPreferences sp;
	String tag;
	int box_index=0;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setTheme(R.style.Holo_Theme_Light); 
		
		//setTheme(R.style.StyledIndicators); 
		//getSupportActionBar().hide();
		
		//requestWindowFeature(Window.FEATURE_NO_TITLE);
		// setSupportProgressBarVisibility(false);
		aq = new AQuery(this);
		setContentView(R.layout.activity_main);
		fm=getSupportFragmentManager();
		
	    title = aq.id(R.id.main_top).id(R.id.main_top_title).getTextView(); 
	    title_r = aq.id(R.id.main_top).id(R.id.main_top_r).getView(); 
	    
		one = (TextView) aq.id(R.id.main_bott_one).clicked(this).getView();
		two = (TextView) aq.id(R.id.main_bott_two).clicked(this).getView();
		three = (TextView) aq.id(R.id.main_bott_three).clicked(this).getView();
		four = (TextView) aq.id(R.id.main_bott_four).clicked(this).getView();
		five = (TextView) aq.id(R.id.main_bott_five).clicked(this).getView();
		aq.id(R.id.main_top_r).clicked(this);
		
		Bundle b=getIntent().getExtras();
		if (b!=null) {
			tag=b.getString("tag");
			if (tag.equals("box")) {
				aq.id(R.id.main_bott_four).click();
			}else if(tag.equals("profile")){
				aq.id(R.id.main_bott_five).click();
			}
		}else{
			aq.id(R.id.main_bott_one).click();
			
		}
		
		
		
		
		
		//  mLocations = getResources().getStringArray(R.array.locations);

	        /*Context context = getSupportActionBar().getThemedContext();
	      
	        ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(context, R.array.locations, R.layout.sherlock_spinner_item);
	        list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

	        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
	        getSupportActionBar().setListNavigationCallbacks(list, this);*/
	}

	
	public void init(){
		//aq.ajax(url, String.class, this, "parsedata");
	}
	
	
	
	
   
	
	
	public AQuery getAq() {
		return aq;
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {  
        case R.id.main_bott_one:  
            // 当点击了消息tab时，选中第1个tab
        	title.setText(one.getText());
        	one.setTextColor(Color.BLUE);
        	two.setTextColor(Color.WHITE);
        	three.setTextColor(Color.WHITE);
        	four.setTextColor(Color.WHITE);
        	five.setTextColor(Color.WHITE);
            setTabSelection(0);  
            break;  
        case R.id.main_bott_two:  
            // 当点击了联系人tab时，选中第2个tab 
        	title.setText(two.getText());
        	one.setTextColor(Color.WHITE);
        	two.setTextColor(Color.BLUE);
        	three.setTextColor(Color.WHITE);
        	four.setTextColor(Color.WHITE);
        	five.setTextColor(Color.WHITE);
            setTabSelection(1);  
            break;  
        case R.id.main_bott_three:  
            // 当点击了动态tab时，选中第3个tab  
        	title.setText(three.getText());
        	one.setTextColor(Color.WHITE);
        	two.setTextColor(Color.WHITE);
        	three.setTextColor(Color.BLUE);
        	four.setTextColor(Color.WHITE);
        	five.setTextColor(Color.WHITE);
            setTabSelection(2);  
            break;  
        case R.id.main_bott_four:  
            // 当点击了设置tab时，选中第4个tab  
        	
            setTabSelection(3);  
            break;  
        case R.id.main_bott_five:  
        	// 当点击了设置tab时，选中第4个tab  
        	
        	setTabSelection(4);  
        	break;  
        case R.id.main_top_r:        	
        	PopupMenu menu = new PopupMenu(MainActivity.this,title_r);       	
            makeMenu(menu.getMenu(), getMenuInflater());
            menu.show();
         break;
            
        default:  
            break;  
        }  
		
	}
	
	public void setTabSelection(int index){
        FragmentTransaction transaction = fm.beginTransaction();   
        
		switch (index) {  
        case 0:  
        	hideFragments(transaction); 
        	if (onef == null) {  
        		 onef = new  OneFragment();  
                transaction.add(R.id.main_container, onef);  
            } else {            
                transaction.show(onef);  
            } 
            
        	break;
        case 1:  
        	hideFragments(transaction); 
        	if (twof == null) {  
        		twof = new  TwoFragment();  
                transaction.add(R.id.main_container, twof);  
            } else {  
                transaction.show(twof);  
            } 
        	break;
        case 2:  
        	hideFragments(transaction); 
        	if (threef == null) {  
        		threef = new  ThreeFragment();  
                transaction.add(R.id.main_container, threef);  
            } else {  
                transaction.show(threef);  
            } 
        	System.out.println("threef==========================");
        	break;
        case 3:  
        	if (sp==null) {
        		sp=((Application)getApplication()).getSharedPreferences("whm",Context.MODE_PRIVATE);
			}
        	String cookiename=sp.getString("cookie_name",null);
        	String cookievalue=sp.getString("cookie_value",null);
        	//System.out.println("mainactivity cookiename="+cookiename);
        	//System.out.println("mainactivity cookievalue="+cookievalue);
        	if (cookiename!=null&&cookievalue!=null) {
        		title.setText(four.getText());
            	one.setTextColor(Color.WHITE);
            	two.setTextColor(Color.WHITE);
            	three.setTextColor(Color.WHITE);
            	four.setTextColor(Color.BLUE);
            	five.setTextColor(Color.WHITE);
            	hideFragments(transaction); 
            	 title_r.setVisibility(View.VISIBLE);
        		if (onebox == null) { 
        		//	Toast.makeText(MainActivity.this, "fourf == null", 1000).show();
        			onebox = new  FourFragmentOrder();  
        			
                   transaction.add(R.id.main_container, onebox);  
            } else {  
            	//Toast.makeText(MainActivity.this, "fourf != null", 1000).show();
            	title_r.setVisibility(View.VISIBLE);
            	switch (box_index) {  
                case 0:  
                	//onebox.set_func();
                	 title.setText("order");
                	
                    transaction.show(onebox); 
                    if (Whm_app.newl.contains(1)) {
                    	onebox.refresh();
 					}
                	break;
                case 1:  
                	//twobox.set_func();
                	 title.setText("preorder");
	                transaction.show(twobox); 
	                if (Whm_app.newl.contains(2)) {
	                	twobox.refresh();
 					}
                	break;
                case 2:  
                	//twobox.set_func();
                	 title.setText("subscribe");
	                transaction.show(threebox); 
	                if (Whm_app.newl.contains(3)) {
 	                	threebox.refresh();
 					}
                	break;	
                default:  
                    break; 	
        		} 	
            	
            }
        		
			}else{
				Bundle b=new Bundle();
				b.putString("tag", "box");				
				Intent i=new Intent(MainActivity.this, Login.class);
			    i.putExtras(b);
				startActivity(i);
			}
        	break;
         case 4:  
        	 if (sp==null) {
         		sp=((Application)getApplication()).getSharedPreferences("whm",Context.MODE_PRIVATE);
 			}
        	String cookiename2=sp.getString("cookie_name",null);
        	String cookievalue2=sp.getString("cookie_value",null);
        	//System.out.println("mainactivity cookiename="+cookiename);
        	//System.out.println("mainactivity cookievalue="+cookievalue);
        	if (cookiename2!=null&&cookievalue2!=null) {
        		title.setText(five.getText());
            	one.setTextColor(Color.WHITE);
            	two.setTextColor(Color.WHITE);
            	three.setTextColor(Color.WHITE);
            	four.setTextColor(Color.WHITE);
            	five.setTextColor(Color.BLUE);
            	hideFragments(transaction); 
        		if (fivef == null) {  
        			
        			fivef = new  FiveFragment();  
                transaction.add(R.id.main_container, fivef);  
            } else {  
            	
                transaction.show(fivef);  
            }
        		
			}else{
				Bundle b=new Bundle();
				b.putString("tag", "profile");				
				Intent i=new Intent(MainActivity.this, Login.class);
			    i.putExtras(b);
				startActivity(i);
			}
        	break;	
         case 5:
        	 hideFragments(transaction); 
        	 title_r.setVisibility(View.VISIBLE);
        	 box_index=0;
	        	System.out.println("===================== showf 1");
	        	if (onebox == null) {  
	        		onebox = new  FourFragmentOrder();  
	                transaction.add(R.id.main_container, onebox);  
	            } else {  
	            	//twobox.set_func();
	            	if (Whm_app.newl.contains(1)) {
	            		onebox.refresh();
					}
	                transaction.show(onebox);  
	            } 
	         break;	
         case 6:
        	 hideFragments(transaction); 
        	 title_r.setVisibility(View.VISIBLE);
        	 box_index=1;
	        	System.out.println("===================== showf 1");
	        	if (twobox == null) {  
	        		twobox = new  FourFragmentPreorder();  
	                transaction.add(R.id.main_container, twobox);  
	            } else {  
	            	//twobox.set_func();
	                transaction.show(twobox); 
	                if (Whm_app.newl.contains(2)) {
	                	 twobox.refresh();
					}
	               
	            } 
	         break;	
         case 7:
        	 hideFragments(transaction); 
        	
        	 title_r.setVisibility(View.VISIBLE);
        	 box_index=2;
	        	System.out.println("===================== showf 1");
	        	if (threebox == null) {  
	        		threebox = new  FourFragmentSubscribe();  
	                transaction.add(R.id.main_container, threebox);  
	            } else {  
	            	//twobox.set_func();
	                transaction.show(threebox);  
	                if (Whm_app.newl.contains(3)) {
	                	threebox.refresh();
					}
	            } 
	         break;	
        default:  
            break; 	
		}
		transaction.commit();
		
	}

	@Override
	public void show_boxf(int pos) {
		// TODO Auto-generated method stub
		//Toast.makeText(this, String.valueOf(pos), 1000).show();
			switch (pos) {  
	        case 0:
	        	
	        	 setTabSelection(5);
	        	break;
	        case 1:
	        	
	        	setTabSelection(6);
	        	break;
	        case 2:
	        	setTabSelection(7);
	        	break;
	        default:  
	            break; 
			}     
			
		
	}


	private void hideFragments(FragmentTransaction transaction) {  
		title_r.setVisibility(View.GONE);
        if (onef != null) {  
            transaction.hide(onef);  
        }  
        if (twof != null) {  
            transaction.hide(twof);  
        }  
        if (threef != null) {  
            transaction.hide(threef);  
        }  
        if (fourf != null) {  
            transaction.hide(fourf);  
        }  
        if (fivef != null) {  
            transaction.hide(fivef);  
        }  
        if (onebox != null) {  
        	
        	transaction.hide(onebox);  
        }  
        if (twobox != null) {  
        	
        	transaction.hide(twobox);  
        }  
        if (threebox != null) {  
        	transaction.hide(threebox);  
        }  
       
    }
	
	 public void makeMenu(Menu menu, MenuInflater inflater) {
	      if (menu instanceof ContextMenu) {
	          ((ContextMenu) menu).setHeaderTitle("Context menu title");
	      }
	      menu.clear();
	      inflater.inflate(R.menu.menu, menu);
	      
	      for (int i = 0; i < menu.size(); i++) {
	          menu.getItem(i).setOnMenuItemClickListener(mOnMenuItemClickListener);
	        
	      }
	     
	  }
	  
	  private final OnMenuItemClickListener mOnMenuItemClickListener = new OnMenuItemClickListener() {
	      @Override
	      public boolean onMenuItemClick(MenuItem menuItem) {
	          boolean result = false;
	          final int id = menuItem.getItemId();
	          if (menuItem.getTitle().equals("order")) {
	        	  title.setText(menuItem.getTitle());
	        	  show_boxf(0);
			  }
	          if (menuItem.getTitle().equals("preorder")) {  
	        	  title.setText(menuItem.getTitle());
	        	  show_boxf(1);
	          }
	          if (menuItem.getTitle().equals("subscribe")) { 
	        	  title.setText(menuItem.getTitle());
	        	  show_boxf(2);
	          }
	          //  System.out.println("onMenuItemClick id========="+id);
	          return true;
	      }
	  };
	

}
